package com.kehongborui.ticket.aop;

import com.kehongborui.ticket.model.UserModel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief 基于注解的aop组件
 * @email 122162005@qq.com
 * @date 2018-08-15
 */
@Component  // 自动扫描的注解
@Aspect     // 声明当前类为切面组件的注解
public class AnnotationOperateLogger {
    @Before("within(com.kehongborui.ticket.controller..*)")
    public void log() {
        System.out.println("这是注解实现的前置通知");
    }

//    @Around("execution(* com.kehongborui.ticket.service..*(..))")
    @Around("execution(* com.kehongborui.ticket.service.LoginService.login(..))")
//    @Around("execution(* com.kehongborui.ticket.controller..*.*(..))")
    public Object handler(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("调用登录方法前,传入的参数" + pjp.getArgs());
        UserModel model = (UserModel) pjp.getArgs()[0];
        int b = (int)pjp.getArgs()[1];
        // 模仿字段加密
        if ("1".equals(model.getPassword())) {
            model.setPassword(model.getPassword() + "23456");
        }

        Object ret = pjp.proceed();
        System.out.println("调用登录方法后,返回的数据是" + ret);
        return ret;
    }
}
